package com.jcl.core.security;

import com.jcl.core.security.entity.ResultEnum;
import com.jcl.core.security.utils.ResultUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName EntryPointUnauthorizedHandler
 * @description: 用户未登录时返回给前端的数据 -- 401
 * @Author yyliu
 * @Date 2018/12/18 11:15
 * @Version 1.0
 **/
public class EntryPointUnauthorizedHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setStatus(401);
        ResultUtil.failure(response, ResultEnum.USER_NEED_AUTHORITIES.getCode(),
                ResultEnum.USER_NEED_AUTHORITIES.getMessage());


    }
}
